//
//  PS_WangDaiFanLiDetailViewController.m
//  FinanceSupermarket
//
//  Created by 丹丹 on 2018/12/18.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_WangDaiFanLiDetailViewController.h"
#import "PS_WangDaiFanLiDetailTopCell.h"
#import "PS_WangDaiFaiLiGuiZeCell.h"
#import "PS_TouZiXuZhiCell.h"
#import "PS_FanLiDetailModel.h"
#import "PS_WebViewController.h"
@interface PS_WangDaiFanLiDetailViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    PS_FanLiDetailModel * detailModel;
}
@property (nonatomic, strong)UITableView * listTableView;
@property (nonatomic, strong)UIView * footerView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)NSMutableArray * ruleArray;
@end

@implementation PS_WangDaiFanLiDetailViewController

- (NSMutableArray *)ruleArray{
    
    if (_ruleArray == nil) {
        
        _ruleArray = [NSMutableArray array];
    }
    return _ruleArray;
}
- (UIView *)headerView{
    
    if (_headerView == nil) {
        
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
        UILabel * conLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH-30, 80)];
        conLabel.numberOfLines = 2;
        conLabel.text = @"市场有风险，投资需谨慎\n该商家投资产品的本金及收益兑付与财信无关";
        conLabel.font = [UIFont systemFontOfSize:15];
        [_headerView addSubview:conLabel];
        
    }
    
    return _headerView;
    
}

- (UIView *)footerView{
    
    if (_footerView == nil) {
        
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(30, 30, SCREEN_WIDTH-60, 45);
        [button setTitle:@"投资抢返利" forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"dengluzhuceanniu"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(pushWeb:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:button];
    }
    
    return _footerView;
    
}
- (UITableView *)listTableView{
    
    if (_listTableView == nil) {
        
        _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight, SCREEN_WIDTH,SCREEN_HEIGHT-SafeAreaBottomHeight-SafeAreaTopHeight)];
        _listTableView.dataSource = self;
        _listTableView.delegate = self;
        _listTableView.showsVerticalScrollIndicator= NO;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.scrollEnabled = YES;
        _listTableView.tableHeaderView = self.headerView;
        _listTableView.tableFooterView = self.footerView;
    }
    
    return _listTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"产品详情";
    [self.view addSubview:self.listTableView];
    
    [self getDetailData];
    // Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
    }else if (section == 1){
        
        return self.ruleArray.count;
    }else{
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 0;
        
    }else if (section ==1){
        
        return 50;
        
    }else{
        
        return 60;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 1){
        
        return 65;
        
    }else{
        
        self.listTableView.rowHeight = UITableViewAutomaticDimension;
        self.listTableView.estimatedRowHeight = 44;
        return self.listTableView.rowHeight;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * backView;
    
    if (section == 1) {
        
        backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        backView.backgroundColor = [UIColor whiteColor];
        
        UILabel * markLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 12.5, 100, 25)];
        markLabel.text = @"返利规则";
        markLabel.userInteractionEnabled = YES;
        [backView addSubview:markLabel];
        
        UIImageView * arrowIamge = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 15-7, 17.5, 7, 15)];
        arrowIamge.image = [UIImage imageNamed:@"xiaofanhui"];
        arrowIamge.userInteractionEnabled = YES;
        [backView addSubview:arrowIamge];
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
        button.tag = section;
        [button addTarget:self action:@selector(pushNext:) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:button];
        
        UIView * line2 = [[UIView alloc]initWithFrame:CGRectMake(15,49, SCREEN_WIDTH-30, 1)];
        line2.backgroundColor = [UIColor colorWithHexString:@"efefef"];
        [backView addSubview:line2];
        
        
    }

    
    if (section == 2) {
        
        backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        backView.backgroundColor = [UIColor whiteColor];
        
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        line.backgroundColor = [UIColor colorWithHexString:@"efefef"];
        [backView addSubview:line];
        
        UILabel * markLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 22.5, 100, 25)];
        markLabel.text = @"投资须知";
        markLabel.userInteractionEnabled = YES;
        [backView addSubview:markLabel];
        
        UIImageView * arrowIamge = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 15-7, 27.5, 7, 15)];
        arrowIamge.image = [UIImage imageNamed:@"xiaofanhui"];
        arrowIamge.userInteractionEnabled = YES;
        [backView addSubview:arrowIamge];
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 10, SCREEN_WIDTH, 50);
        button.tag = section;
        [button addTarget:self action:@selector(pushNext:) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:button];
        
        UIView * line2 = [[UIView alloc]initWithFrame:CGRectMake(15,59, SCREEN_WIDTH-30, 1)];
        line2.backgroundColor = [UIColor colorWithHexString:@"efefef"];
        [backView addSubview:line2];
        

    }
    
    return backView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section ==0) {
        
        PS_WangDaiFanLiDetailTopCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_WangDaiFanLiDetailTopCell"];
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle]loadNibNamed:@"PS_WangDaiFanLiDetailTopCell" owner:self options:nil]objectAtIndex:0];
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.timeLabel.text = detailModel.end_time;
        [cell.logoImage sd_setImageWithURL:[NSURL URLWithString:detailModel.logo]];
        cell.contentLabel.text = detailModel.description2;
        [cell.openDetailButton addTarget:self action:@selector(pushWeb:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }else if (indexPath.section == 1){
        
        PS_FanLiDetailModel * model = [self.ruleArray objectAtIndex:indexPath.row];
        PS_WangDaiFaiLiGuiZeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_WangDaiFaiLiGuiZeCell"];
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle]loadNibNamed:@"PS_WangDaiFaiLiGuiZeCell" owner:self options:nil]objectAtIndex:0];
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.timeLabel.text = [NSString stringWithFormat:@"%@个月",model.period];
        cell.moneyLabel.text = model.money;
        cell.fanXianLabel.text = model.rebate_description;
        return cell;
    }else{
        
        PS_TouZiXuZhiCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_TouZiXuZhiCell"];
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle]loadNibNamed:@"PS_TouZiXuZhiCell" owner:self options:nil]objectAtIndex:0];
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSAttributedString * htmlStr = [[NSAttributedString alloc] initWithData:[detailModel.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        cell.contentLabel.attributedText = htmlStr;
        return cell;
    }
   
}

- (void)pushWeb:(UIButton *)sender{

    PS_WebViewController * VC = [[PS_WebViewController alloc]init];
    VC.webUrl = detailModel.url;
    VC.isFrom = 4;
    [self.navigationController pushViewController:VC animated:YES];
    
}
- (void)pushNext:(UIButton *)sender{
    
    
}

- (void)getDetailData{
    
    [Utils post:PROJECTDETAIL parameters:@{@"merchant_id":_merchant_id} success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        NSDictionary * merchant_infoDic = [dataDic objectForKey:@"merchant_info"];
        self->detailModel = [PS_FanLiDetailModel mj_objectWithKeyValues:merchant_infoDic];
        self->detailModel.pID = [merchant_infoDic objectForKey:@"id"];
        self->detailModel.description2 = [merchant_infoDic objectForKey:@"description"];
        NSArray * ruleArr = [dataDic objectForKey:@"rebate_rule"];
        
        for (NSDictionary * dic in ruleArr) {
            
            PS_FanLiDetailModel * model = [PS_FanLiDetailModel mj_objectWithKeyValues:dic];
            model.rID = [dic objectForKey:@"id"];
            [self.ruleArray addObject:model];
        }
        
        [self.listTableView reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
